﻿using Gx.Bll.Manage;
using Gx.Common;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Mvc;

namespace Gx.ColdWarehouseErp.Controllers
{
    public class ReportController : BaseController
    {
        protected LKCustomerReportBll bll = new LKCustomerReportBll();
        /// <summary>
        /// 客户存货汇总报表
        /// </summary>
        /// <returns></returns>
        public ActionResult CustomerInventorySummaryReport()
        {
            return View();
        }

        /// <summary>
        /// 获取客户存货汇总报表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public JsonResult GetCustomerInventorySummaryReport(CustomerInventorySummaryInput input)
        {
            var list = bll.GetCustomerInventorySummaryReport(input);
            var footer = new { Total = list.Sum(s => s.Total), TotalTon = list.Sum(s => s.TotalTon) };
            List<dynamic> footers = new List<dynamic>() { footer };
            return JsonList(list.Count(), list.ToArray(), footers.ToArray());
        }

        /// <summary>
        /// 导出客户存货汇总报表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult ExportCustomerInventorySummaryReport(int? houseId, DateTime? summartTime)
        {
            CustomerInventorySummaryInput input = new CustomerInventorySummaryInput
            {
                SummaryTime = summartTime,
                WarehouseId = houseId
            };
            var list = bll.GetCustomerInventorySummaryReport(input);
            var footer = new CustomerInventorySummaryDto { Total = list.Sum(s => s.Total), TotalTon = list.Sum(s => s.TotalTon) };

            list.Add(footer);

            List<string> header = new List<string> { "客户编号", "客户名称", "存货件数", "存货吨重" };
            var bytes = ExcelHelper.ExportToExcel(list, "客户存货汇总报表", header);
            return File(bytes, "application/vnd.ms-excel", Url.Encode("客户存货汇总报表-" + DateTime.Now.ToString("yyyyMMddmmddss") + ".xlsx"));
        }

        //客户存货明细表
        public ActionResult CustomerInventoryDetailReport()
        {
            return View();
        }

        //获取客户存货明细表
        public JsonResult GetCustomerInventoryDetailReport(CustomerInventoryDetailInput input)
        {
            var list = bll.GetCustomerInventoryDetailReport(input);
            var footer = new CustomerInventoryDetailDto { Total = list.Sum(s => s.Total), TotalTon = list.Sum(s => s.TotalTon) };
            List<CustomerInventoryDetailDto> footers = new List<CustomerInventoryDetailDto>() { footer };
            return JsonList(list.Count(), list.ToArray(), footers.ToArray());
        }

        //导出客户存货明细表
        public ActionResult ExportCustomerInventoryDetailReport(DateTime? summartTime, int? customerid, int? houseid,string goodscode,string goodsname,string goodsspec,int? rentalwarehousetype)
        {
            CustomerInventoryDetailInput input = new CustomerInventoryDetailInput
            {
                SummaryTime = summartTime,
                WarehouseId = houseid,
                CustomerId = customerid,
                GoodsCode=goodscode,
                GoodsName=goodsname,
                GoodsSpec=goodsspec,
                RentalWarehouseType=rentalwarehousetype
            };
            var list = bll.GetCustomerInventoryDetailReport(input);
            var footer = new CustomerInventoryDetailDto { Total = list.Sum(s => s.Total), TotalTon = list.Sum(s => s.TotalTon) };

            list.Add(footer);
            List<string> header = new List<string> { "客户编号", "客户名称", "商品批号", "商品名称", "规格", "仓库", "柜号", "生产日期", "过期日期", "产地", "备注", "件重(kg)", "存货件数", "存货托盘数/面积", "存货吨重", "入库时间", "租赁类型" };
            var bytes = ExcelHelper.ExportToExcel(list, "客户存货明细表", header);
            return File(bytes, "application/vnd.ms-excel", Url.Encode("客户存货明细表-" + DateTime.Now.ToString("yyyyMMddmmddss") + ".xlsx"));
        }
    }
}